package com.github.wj.security.server.service.impl;
import com.github.wj.common.security.util.DateFormatUtils;
import com.github.wj.common.security.util.EntityUtil;
import com.github.wj.common.security.util.IConstants;
import com.github.wj.orm.security.entity.Pager;
import com.github.wj.orm.security.repository.impl.CustomRepositoryPager;
import com.github.wj.security.server.controller.BaseService;
import com.github.wj.security.server.domain.TLoggerManageRepository;
import com.github.wj.security.server.entity.TBsdtLogger;
import com.github.wj.security.server.service.TLoggerManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by wangj on 2017/7/13.
 */
@Service
public class TLoggerManageServiceImpl  extends BaseService implements TLoggerManageService {
    private static final Logger LOG= LoggerFactory.getLogger(TLoggerManageServiceImpl.class);

    @Autowired
    private TLoggerManageRepository repository;

    @Override
    public Page<TBsdtLogger> findAll(Pager pager, TBsdtLogger logger) {
        Pageable pageable= CustomRepositoryPager.pageable(pager);
        if(null!=logger){
            return repository.findAll(new Specification<TBsdtLogger>(){
                @Override
                public Predicate toPredicate(Root<TBsdtLogger> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                    List<Predicate> list = new ArrayList<Predicate>();
                    if(!StringUtils.isEmpty(logger.getRequestDateFmt())){
                        try {
                            Date startDate= DateFormatUtils.parse(logger.getRequestDateFmt(),"yyyy/MM/dd HH:mm:ss");
                            Date endDate=new Date();
                            if(!StringUtils.isEmpty(logger.getRequestDateEndFmt())){
                                endDate=DateFormatUtils.parse(logger.getRequestDateEndFmt(),"yyyy/MM/dd HH:mm:ss");
                            }
                            list.add(cb.between(root.<Date>get("requestDate"),startDate,endDate));
                        } catch (ParseException e) {
                            LOG.error(e.getLocalizedMessage());
                        }
                    }
                    if(!StringUtils.isEmpty(logger.getLoginid())){
                        list.add(cb.like(root.get("loginid").as(String.class),"%"+logger.getLoginid()+"%"));
                    }
                    Predicate[] p = new Predicate[list.size()];
                    return cb.and(list.toArray(p));
                }
            },pageable);
        }
        return repository.findAll(pageable);
    }

    @Override
    @Transactional
    public TBsdtLogger save(TBsdtLogger logger) {
        if(null==logger.getId()){
            logger.setId(sequenceService.returnId(logger.getClass().getSimpleName()));
        }
        return repository.save(logger);
    }

    @Transactional
    public int delete(String ids) {
        List<Long> id= EntityUtil.getIterable(IConstants.LONG,ids);
        int i=0;
        if(null!=id&&id.size()>0){
            for (Long l:id){
                repository.delete(l);
                i++;
            }
        }
        return i;
    }
}
